"""
Case Type   : 人为因素
Case Name   : 多线程执行alter system修改不同参数
Create At   : 2022/12/28
Owner       : li-xin12345
Description :
    1、开启3个线程,同步执行alter system set 修改guc参数
    2、重启数据库,查询修改是否生效
    3、恢复参数
Expect      :
    1、成功
    2、重启库成功,修改均生效
    3、成功
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread


class ModifyConnection(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonshpri = CommonSH('PrimaryDbUser')
        result = self.commonshpri.executDbSql("show enable_ustore;")
        self.log.info(result)
        self.enable_ustore = str(result.splitlines()[-2]).strip()
        result = self.commonshpri.executDbSql("show wal_keep_segments;")
        self.log.info(result)
        self.wal_keep_segments = str(result.splitlines()[-2]).strip()
        result = self.commonshpri.executDbSql("show post_auth_delay;")
        self.log.info(result)
        self.post_auth_delay = str(result.splitlines()[-2]).strip()

    def test_modify_config(self):
        text = "--step1:开启3个线程,同步执行alter system set 修改guc参数 " \
               "expect:成功--"
        self.log.info(text)
        sql_cmd1 = 'alter system set enable_ustore to on;'
        thread1 = ComThread(self.commonshpri.executDbSql, args=(sql_cmd1,))
        thread1.setDaemon(True)

        sql_cmd2 = 'alter system set wal_keep_segments to 3;'
        thread2 = ComThread(self.commonshpri.executDbSql, args=(sql_cmd2,))
        thread2.setDaemon(True)

        sql_cmd3 = 'alter system set post_auth_delay to 1;'
        thread3 = ComThread(self.commonshpri.executDbSql, args=(sql_cmd3,))
        thread3.setDaemon(True)

        thread1.start()
        thread2.start()
        thread3.start()
        thread1.join(10)
        thread2.join(10)
        thread3.join(10)
        result = []
        result1 = thread1.get_result()
        self.log.info(result1)
        result2 = thread2.get_result()
        self.log.info(result2)
        result3 = thread3.get_result()
        self.log.info(result3)
        result.append(result1 + result2 + result3)
        self.log.info(result)
        self.assertEqual(str(result).count('ALTER SYSTEM SET'), 3, '执行失败'
                         + text)

        text = "step2:重启数据库,查询修改是否生效 expect:重启库成功,修改均生效"
        self.log.info(text)
        result = self.commonshpri.stopDbCluster(get_detail=True)
        self.log.info(result)
        result1 = self.commonshpri.startDbCluster(get_detail=True)
        self.log.info(result1)
        self.assertTrue(result, '执行失败' + text)
        self.assertTrue(result1, '执行失败' + text)
        result = self.commonshpri.executDbSql(
            "show enable_ustore;show wal_keep_segments;show post_auth_delay;")
        self.log.info(result)
        self.assertEqual(str(result.splitlines()[-2]).strip(), '1s', '执行失败'
                         + text)
        self.assertEqual(str(result.splitlines()[-7]).strip(), '3', '执行失败'
                         + text)
        self.assertEqual(str(result.splitlines()[2]).strip(), 'on', '执行失败'
                         + text)

    def tearDown(self):
        text = "step3:恢复参数 expect:成功"
        self.log.info(text)
        result = self.commonshpri.executDbSql(
            f"alter system set enable_ustore to {self.enable_ustore};")
        self.log.info(result)
        result1 = self.commonshpri.executDbSql(
            f"alter system set wal_keep_segments to {self.wal_keep_segments};")
        self.log.info(result1)
        result2 = self.commonshpri.executDbSql(
            f"alter system set post_auth_delay to {self.post_auth_delay};")
        self.log.info(result2)
        result3 = self.commonshpri.stopDbCluster(get_detail=True)
        self.log.info(result3)
        result4 = self.commonshpri.startDbCluster(get_detail=True)
        self.log.info(result4)
        self.assertTrue(result3, '执行失败' + text)
        self.assertTrue(result4, '执行失败' + text)
        self.assertIn('ALTER SYSTEM SET', result, '执行失败' + text)
        self.assertIn('ALTER SYSTEM SET', result1, '执行失败' + text)
        self.assertIn('ALTER SYSTEM SET', result2, '执行失败' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
